<?php

/**
 * This is the model class for table "xman_comment".
 *
 * The followings are the available columns in table 'xman_comment':
 * @property integer $comment_id
 * @property string $comment_content
 * @property string $comment_posted_date
 * @property integer $comment_posted_by_user_id
 * @property string $comment_last_edited_date
 * @property integer $comment_project_id
 * @property integer $comment_object_id
 * @property string $comment_object_type
 *
 * The followings are the available model relations:
 * @property User $commentPostedByUser
 * @property Project $commentProject
 */
class Comment extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Comment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'xman_comment';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('comment_content, comment_posted_by_user_id, comment_project_id, comment_object_id, comment_object_type', 'required'),
			array('comment_posted_by_user_id, comment_project_id, comment_object_id', 'numerical', 'integerOnly'=>true),
			array('comment_content', 'length', 'max'=>5000),
			array('comment_object_type', 'length', 'max'=>20),
			array('comment_posted_date, comment_last_edited_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('comment_id, comment_content, comment_posted_date, comment_posted_by_user_id, comment_last_edited_date, comment_project_id, comment_object_id, comment_object_type', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'commentPostedByUser' => array(self::BELONGS_TO, 'User', 'comment_posted_by_user_id'),
			'commentProject' => array(self::BELONGS_TO, 'Project', 'comment_project_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'comment_id' => 'Comment',
			'comment_content' => 'Comment Content',
			'comment_posted_date' => 'Comment Posted Date',
			'comment_posted_by_user_id' => 'Comment Posted By User',
			'comment_last_edited_date' => 'Comment Last Edited Date',
			'comment_project_id' => 'Comment Project',
			'comment_object_id' => 'Comment Object',
			'comment_object_type' => 'Comment Object Type',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('comment_id',$this->comment_id);
		$criteria->compare('comment_content',$this->comment_content,true);
		$criteria->compare('comment_posted_date',$this->comment_posted_date,true);
		$criteria->compare('comment_posted_by_user_id',$this->comment_posted_by_user_id);
		$criteria->compare('comment_last_edited_date',$this->comment_last_edited_date,true);
		$criteria->compare('comment_project_id',$this->comment_project_id);
		$criteria->compare('comment_object_id',$this->comment_object_id);
		$criteria->compare('comment_object_type',$this->comment_object_type,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
        
        /**
         * Lay thoi gian so voi thuc tai
         */
        public function spaceTime(){
                $now = strtotime('now');
                $datetime = $this->comment_posted_date;
                if ($this->comment_last_edited_date != null)
                        $datetime = $this->comment_last_edited_date;
                return Html::spaceTime($datetime);
                
                $timestamp = CDateTimeParser::parse($datetime,'yyyy-MM-dd H:m:s');
                $space = $now - $timestamp;
                if ($space <60 )
                    return "about ".number_format($space,0)." seconds ago";
                
                $space = $space / 60;
                if ($space <60 )
                    return "about ".number_format($space,0)." minutes ago";
                
                $space = $space / 60;
                if ($space <24 )
                    return "about ".number_format($space,0)." hours ago";
                
                $space = $space / 24;
                if ($space <7 )
                    return "about ".number_format($space,0)." days ago";
                
                $space = $space / 7;
                if ($space <4 )
                    return "about ".number_format($space,0)." weeks ago";
                
                $space = $space / 4;
                return "about ".number_format($space,0)." moths ago";
        }
}